global class SPObjsWS {
/*
  //
  // Shared classes
  //

  global class AccountFields {
      WebService String Name;
      WebService Id aId;
      public AccountFields(Id aId, String Name) { this.Name=Name; this.aId=aId; }
  }

  global class EntFields {
     WebService String Name;
     WebService String BHName;
     WebService Boolean IsDefault;
     public EntFields(String Name,String BHName,Boolean IsDefault) {
         this.Name = Name;
         this.BHname = BHName;
         this.IsDefault = IsDefault;
     }
  }
  
  //
  // MASP import/export
  //
  
  global class MASPObjs {
     WebService String Name;
     WebService String[] PxNameArr;
     WebService Decimal[] TimesArr;
     WebService AccountFields[] accounts;     
     WebService EntFields[] entitlements;
  }
  
  WebService static MASPObjs retrieveMASPObjs(Id spId) {
      MASPObjs m = new MASPObjs();
      Support_Program__c sp = [Select Name,P1__c,P2__c,P3__c,P4__c,P5__c,
            P1_Resolution_Hours__c,
            P1_Response_Hours__c,
            P1_Workaround_Hours__c,
            P2_Resolution_Hours__c,
            P2_Response_Hours__c,
            P2_Workaround_Hours__c,
            P3_Resolution_Hours__c,
            P3_Response_Hours__c,
            P3_Workaround_Hours__c,
            P4_Resolution_Hours__c,
            P4_Response_Hours__c,
            P4_Workaround_Hours__c,
            P5_Resolution_Hours__c,
            P5_Response_Hours__c,
            P5_Workaround_Hours__c,
          (Select Name,Business_Hours__r.Name,IsDefault__c From Entitlements__r) From Support_Program__c Where Id=:spId];
      m.Name = sp.Name;
      m.PxNameArr = new String[]{ sp.P1__c, sp.P2__c, sp.P3__c, sp.P4__c, sp.P5__c };
      m.TimesArr = new Decimal[]{ sp.P1_Response_Hours__c, sp.P1_Workaround_Hours__c, sp.P1_Resolution_Hours__c,
        sp.P2_Response_Hours__c, sp.P2_Workaround_Hours__c, sp.P2_Resolution_Hours__c,
        sp.P3_Response_Hours__c, sp.P3_Workaround_Hours__c, sp.P3_Resolution_Hours__c,
        sp.P4_Response_Hours__c, sp.P4_Workaround_Hours__c, sp.P4_Resolution_Hours__c,
        sp.P5_Response_Hours__c, sp.P5_Workaround_Hours__c, sp.P5_Resolution_Hours__c };
             
      m.entitlements = new EntFields[sp.Entitlements__r.size()];
      for(Integer i = 0; i < sp.Entitlements__r.size(); i++) {
         EntFields ent = new EntFields(sp.Entitlements__r[i].Name,sp.Entitlements__r[i].Business_Hours__r.Name,
             sp.Entitlements__r[i].IsDefault__c);
         m.entitlements[i] = ent;
      }
      
      List<AccountFields> lstAcctFlds = new List<AccountFields>();
      for(Account a : [Select Id,Name From Account Where Support_Program__c=:spId]) {
          lstAcctFlds.add( new AccountFields(a.Id,a.Name) );
      }
      m.accounts = lstAcctFlds;
      return m;
  }

  WebService static Support_Program__c mkMASP(MASPObjs masp) {  
    Support_Program__c sp = new Support_Program__c( RecordTypeId=RT.MASPId );
    sp.Apply_To_All_Contacts_In_Account__c = true;
    sp.Name = masp.Name;
    
    if ((masp.PxNameArr != null)&&(masp.PxNameArr.size()==5)) {
        sp.P1__c = masp.PxNameArr[0];
        sp.P2__c = masp.PxNameArr[1];
        sp.P3__c = masp.PxNameArr[2];
        sp.P4__c = masp.PxNameArr[3];
        sp.P5__c = masp.PxNameArr[4];
    }
    
    if ((masp.TimesArr != null)&&(masp.TimesArr.size()==15)) {
        sp.P1_Response_Hours__c = masp.TimesArr[0];
        sp.P1_Workaround_Hours__c = masp.TimesArr[1];
        sp.P1_Resolution_Hours__c = masp.TimesArr[2];
        
        sp.P2_Response_Hours__c = masp.TimesArr[3];
        sp.P2_Workaround_Hours__c = masp.TimesArr[4];
        sp.P2_Resolution_Hours__c = masp.TimesArr[5];
        
        sp.P3_Response_Hours__c = masp.TimesArr[6];
        sp.P3_Workaround_Hours__c = masp.TimesArr[7];
        sp.P3_Resolution_Hours__c = masp.TimesArr[8];
        
        sp.P4_Response_Hours__c = masp.TimesArr[9];
        sp.P4_Workaround_Hours__c = masp.TimesArr[10];
        sp.P4_Resolution_Hours__c = masp.TimesArr[11];
        
        sp.P5_Response_Hours__c = masp.TimesArr[12];
        sp.P5_Workaround_Hours__c = masp.TimesArr[13];
        sp.P5_Resolution_Hours__c = masp.TimesArr[14];
    }
    insert sp;
    
    if (masp.entitlements != null) {
        List<Entitlement__c> eInsArr = new List<Entitlement__c>();
        for(EntFields e : masp.entitlements) {
            Id BHId = [Select Id From BusinessHours Where Name=:e.BHName].Id;
            Entitlement__c eIns = new Entitlement__c( Name=e.Name,Business_Hours__c=BHId,Support_Program__c=sp.Id,IsDefault__c=e.IsDefault );
            eInsArr.add(eIns);
        }
        insert eInsArr;
    }

    if (masp.accounts != null) {
        List<Account> acctUpArr = new List<Account>();
        List<Account> acctInsArr = new List<Account>();
        for(AccountFields af : masp.accounts) {
            if (af.aId != null) {
                Account aUpd = new Account( Id=af.aId,Support_Program__c=sp.Id );
                acctUpArr.add(aUpd);
            } else {
                Account aIns = new Account( Name=af.Name,Support_Program__c=sp.Id );
                acctInsArr.add(aIns);
            }
        }
        insert acctInsArr;
        update acctUpArr;
    }

    return sp;
  }
  
  
  //
  // NCSP import/export
  //
  
  global class ContactFields {
      WebService String FirstName;
      WebService String LastName;
      WebService Id conId;
      public ContactFields(Id conId, String FirstName, String LastName) { 
          this.FirstName=FirstName; this.LastName=LastName; this.conId=conId; 
      }
  }
  
  global class AssetFields {
      WebService String Name;
      WebService Id assId;
      public AssetFields(Id assId, String Name) { this.Name=Name; this.assId=assId; }
  }
  
  global class NCSPObjs {
     WebService String Name;
     WebService String[] PxNameArr;
     WebService Decimal[] TimesArr;
     WebService AccountFields acct;     
     WebService ContactFields[] contacts;          
     WebService AssetFields[] assets;     
     WebService EntFields[] entitlements;
     WebService Boolean ApplyToAll;
  }
  
  WebService static NCSPObjs retrieveNCSPObjs(Id spId) {
      NCSPObjs nobjs = new NCSPObjs();
      Support_Program__c sp = [Select Name,P1__c,P2__c,P3__c,P4__c,P5__c,
            Account__r.Name, Account__r.Id,
            Apply_To_All_Contacts_In_Account__c,
            P1_Resolution_Hours__c,
            P1_Response_Hours__c,
            P1_Workaround_Hours__c,
            P2_Resolution_Hours__c,
            P2_Response_Hours__c,
            P2_Workaround_Hours__c,
            P3_Resolution_Hours__c,
            P3_Response_Hours__c,
            P3_Workaround_Hours__c,
            P4_Resolution_Hours__c,
            P4_Response_Hours__c,
            P4_Workaround_Hours__c,
            P5_Resolution_Hours__c,
            P5_Response_Hours__c,
            P5_Workaround_Hours__c,
          (Select Name,Business_Hours__r.Name,IsDefault__c From Entitlements__r) From Support_Program__c Where Id=:spId];
      nobjs.Name = sp.Name;
      nobjs.PxNameArr = new String[]{ sp.P1__c, sp.P2__c, sp.P3__c, sp.P4__c, sp.P5__c };
      nobjs.TimesArr = new Decimal[]{ sp.P1_Response_Hours__c, sp.P1_Workaround_Hours__c, sp.P1_Resolution_Hours__c,
        sp.P2_Response_Hours__c, sp.P2_Workaround_Hours__c, sp.P2_Resolution_Hours__c,
        sp.P3_Response_Hours__c, sp.P3_Workaround_Hours__c, sp.P3_Resolution_Hours__c,
        sp.P4_Response_Hours__c, sp.P4_Workaround_Hours__c, sp.P4_Resolution_Hours__c,
        sp.P5_Response_Hours__c, sp.P5_Workaround_Hours__c, sp.P5_Resolution_Hours__c };
      nobjs.ApplyToAll = sp.Apply_To_All_Contacts_In_Account__c;
      
      nobjs.acct = new AccountFields( sp.Account__r.Id,sp.Account__r.Name );
             
      nobjs.entitlements = new EntFields[sp.Entitlements__r.size()];
      for(Integer i = 0; i < sp.Entitlements__r.size(); i++) {
         EntFields ent = new EntFields(sp.Entitlements__r[i].Name,sp.Entitlements__r[i].Business_Hours__r.Name,
             sp.Entitlements__r[i].IsDefault__c);
         nobjs.entitlements[i] = ent;
      }
      
      List<ContactFields> lstConFlds = new List<ContactFields>();
      for(Program_Contact__c pc : [Select Contact__r.LastName,Contact__r.FirstName,Contact__r.Id From Program_Contact__c Where Support_Program__c=:spId]) {
          lstConFlds.add( new ContactFields(pc.Contact__r.Id,pc.Contact__r.FirstName,pc.Contact__r.LastName) );
      }
      nobjs.contacts = lstConFlds;
      
      List<AssetFields> lstAssFlds = new List<AssetFields>();
      for(Program_Asset__c pa : [Select Asset__r.Id,Asset__r.Name From Program_Asset__c Where Support_Program__c=:spId]) {
          lstAssFlds.add( new AssetFields(pa.Asset__r.Id,pa.Asset__r.Name) );
      }
      nobjs.assets = lstAssFlds;
            
      return nobjs;
  }

  WebService static Support_Program__c mkNCSP(NCSPObjs nobjs) {
    if (nobjs.acct.aId == null) {
        Account a = new Account(Name=nobjs.acct.Name); 
        insert a;
        nobjs.acct.aId = a.Id;
    }
    
    Support_Program__c sp = new Support_Program__c( RecordTypeId=RT.NCSPId );
    sp.Account__c = nobjs.acct.aId;
    Boolean bIsApplyToAll = (nobjs.ApplyToAll==false) ? false : true; // API error if direct assignment!!
    sp.Apply_To_All_Contacts_In_Account__c = bIsApplyToAll;
    sp.Name = nobjs.Name;
    
    if ((nobjs.PxNameArr != null)&&(nobjs.PxNameArr.size()==5)) {
        sp.P1__c = nobjs.PxNameArr[0];
        sp.P2__c = nobjs.PxNameArr[1];
        sp.P3__c = nobjs.PxNameArr[2];
        sp.P4__c = nobjs.PxNameArr[3];
        sp.P5__c = nobjs.PxNameArr[4];
    }
    
    if ((nobjs.TimesArr != null)&&(nobjs.TimesArr.size()==15)) {
        sp.P1_Response_Hours__c = nobjs.TimesArr[0];
        sp.P1_Workaround_Hours__c = nobjs.TimesArr[1];
        sp.P1_Resolution_Hours__c = nobjs.TimesArr[2];
        
        sp.P2_Response_Hours__c = nobjs.TimesArr[3];
        sp.P2_Workaround_Hours__c = nobjs.TimesArr[4];
        sp.P2_Resolution_Hours__c = nobjs.TimesArr[5];
        
        sp.P3_Response_Hours__c = nobjs.TimesArr[6];
        sp.P3_Workaround_Hours__c = nobjs.TimesArr[7];
        sp.P3_Resolution_Hours__c = nobjs.TimesArr[8];
        
        sp.P4_Response_Hours__c = nobjs.TimesArr[9];
        sp.P4_Workaround_Hours__c = nobjs.TimesArr[10];
        sp.P4_Resolution_Hours__c = nobjs.TimesArr[11];
        
        sp.P5_Response_Hours__c = nobjs.TimesArr[12];
        sp.P5_Workaround_Hours__c = nobjs.TimesArr[13];
        sp.P5_Resolution_Hours__c = nobjs.TimesArr[14];
    }
    insert sp;
    
    if (nobjs.entitlements != null) {
        List<Entitlement__c> eInsArr = new List<Entitlement__c>();
        for(EntFields e : nobjs.entitlements) {
            Id BHId = [Select Id From BusinessHours Where Name=:e.BHName].Id;
            Boolean bIsDef = (e.IsDefault==false) ? false : true; // API error if direct assignment!!
            Entitlement__c eIns = new Entitlement__c( Name=e.Name,Business_Hours__c=BHId,
                Support_Program__c=sp.Id,IsDefault__c=bIsDef );
            eInsArr.add(eIns);
        }
        insert eInsArr;
    }

    if (nobjs.contacts != null) {
        // Make the Contacts if any don't specify ids
        for(ContactFields cf : nobjs.contacts) {
            if (cf.conId==null) { 
                Contact c = new Contact(AccountId=nobjs.acct.aId,FirstName=cf.FirstName,LastName=cf.LastName); 
                insert c;
                cf.conId = c.Id;
            }
        }
        
        // Now make the PCs
        List<Program_Contact__c> pcArr = new List<Program_Contact__c>();
        for(ContactFields cf : nobjs.contacts) {
            Program_Contact__c pc = new Program_Contact__c( Name=cf.FirstName + ' ' + cf.LastName + ' / ' + nobjs.Name,Contact__c=cf.conId, Support_Program__c=sp.Id );
            pcArr.add(pc);
        }
        insert pcArr;
    }

    if (nobjs.assets != null) {
        // Make the Assets if any don't specify ids
        for(AssetFields assf : nobjs.assets) {
            if (assf.assId==null) { 
                Asset ass = new Asset(AccountId=nobjs.acct.aId,Name=assf.Name); 
                insert ass;
                assf.assId = ass.Id;
            }
        }
        
        // Now make the PAs
        List<Program_Asset__c> paArr = new List<Program_Asset__c>();
        for(AssetFields assf : nobjs.assets) {
            Program_Asset__c pa = new Program_Asset__c( Name=assf.Name  + ' / ' + nobjs.Name,Asset__c=assf.assId, Support_Program__c=sp.Id );
            paArr.add(pa);
        }
        insert paArr;
    }
    
    return sp;
  }
*/
}